//
//  AppDelegate.swift
//  TetrisGame
//
//  Created by mac on 2020/11/23.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {


    //MARK: - application
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        //启动图动画
        self.startAnimation()
        
        //输出异常日志
        NSSetUncaughtExceptionHandler { exception in
            print("++++++++++++++++++ RASH ++++++++++++++++++ \n\(exception)")
            print("++++++++++++++++++ Stack Trace ++++++++++++++++++ \n\(exception.callStackSymbols)")
        }
        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    
    //MARK: - lazy load
    /** UIWindow */
    lazy var window: UIWindow? = {
        autoreleasepool {
            // [S] 设置App 主容器
            let _winDown = UIWindow.init(frame: UIScreen.main.bounds)
            _winDown.backgroundColor = K_APP_VIEWCONTROLLER_BACKGROUND_COLOR
            _winDown.rootViewController = self.getNavigationFor(RootViewController: GameViewController.init())
            
            //设置系统主题色
            if #available(iOS 7.0, *) {
                //tint color 为 高亮颜色
                //tint color默认是蓝色，但是通过改变它的颜色你可以立即让app获得自己独特风格
                _winDown.tintColor = K_APP_TINT_COLOR
            }
            // [S] 设置App 主容器
            
            _winDown.makeKeyAndVisible()
            _winDown.makeKey()
            return _winDown
        }
    }()

}


//MARK: - AppDelegate
extension AppDelegate {
    /** 启动图动画 */
    private func startAnimation(){
        
        let infoDictionary:[String:Any] = Bundle.main.infoDictionary!
        let strStoryboardName:String = String.init(format:"%@",infoDictionary["UILaunchStoryboardName"] as! CVarArg)
        
        let storyboard:UIStoryboard = UIStoryboard.init(name: strStoryboardName, bundle: nil)
        let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "LaunchScreen")
        
        var launchView:UIView? = viewController.view
        if launchView != nil {
            autoreleasepool {
                self.window?.addSubview(launchView!)
                
                //执行动画
                UIView.animate(withDuration: 1, animations: {
                    launchView?.alpha = 0.85
                    
                    //图片变大为原来的1.3倍
                    launchView?.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
                }) { (result:Bool) in
                    
                    //动画结束，移除imageView，呈现主界面
                    launchView?.removeFromSuperview()
                    
                    if result {
                        launchView = nil
                    }
                }
            }
        }
    }
    
    
    //设置主界面
    private func getNavigationFor(RootViewController rvt:UIViewController) -> UINavigationController {
        
        autoreleasepool {
            let _navigation = UINavigationController.init(rootViewController: rvt)
            
            _navigation.navigationBar.isHidden = true
            _navigation.toolbar.isHidden = true
            
            return _navigation
        }
        
    }
}
